import Message from 'tdesign-miniprogram/message/index';
const app = getApp()
const {
  login
} = require('../../api/index')
Page({
  data: {
    isCanFast: false,
    IMGBASEURL: app.globalData.IMGBASEURL,
    backUrl: ''
  },
  onCLI() {

    Message.warning({
      context: this,
      offset: [20, 32],
      duration: 1000,
      content: '请先阅读并勾选用户协议和隐私协议',
    });
  },
  getPhoneNumber(e) {
    let _this =this
    wx.showLoading({
      title: '请稍等',
    })
    if (!e.detail.code) return
    if (this.data.isCanFast) {
      login.phoneAndWxLogin({
        client_id: 'House_App',
        scope: 'openid profile house',
        grant_type: 'sms',
        minicode: e.detail.code
      }).then((res) => {
        if (res) {
          wx.setStorageSync('token', `${res.token_type} ${res.access_token}`)
          app.getUser().then((res) => {
            // this.selectComponent('#consent').handlePopup()
            if (res.IsNewUser) {
              app.globalData.sdk.register(res.UserId)
            }
            app.globalData.sdk.loggedin(res.UserId)
            wx.hideLoading()
            app.getUserStatus(this).then((res) => {
              if (res.UserInfoStatus == 0) {
                if (this.data.backUrl) {
                  wx.redirectTo({
                    url: '/' + this.data.backUrl,
                  })
                  return
                }
                if (this.data.isGoHome == 1) {
                  wx.reLaunch({
                    url: '/pages/ai/index',
                  })
                } else {
                  wx.navigateBack()
                }
            
              }
              if (res.UserInfoStatus != 8) {
                app.loadSignalR()
              }
            })

          })
        }else{
          wx.showToast({
            title: '一键登录失败，请使用手机号登录',
            icon:'none'
          })
        }
      }).catch(()=>{
        wx.showToast({
          title: '一键登录失败，请使用手机号登录',
          icon:'none'
        })
      })
    }
    // login.getphone({code:e.detail.code}).then(res=>{
    //   this.setData({
    //     phoneNumber:res.phoneNumber
    //   })
    // })
  },
  // onConsentClose() {
  //   wx.reLaunch({
  //     url: '/pages/home/index',
  //   })
  // },
  fastLogin() {

    if (this.data.isCanFast) {
      wx.showLoading()
      app.wxfastLogin().then((res) => {
        if (res) {
          // this.selectComponent('#consent').handlePopup()
          wx.reLaunch({
            url: '/pages/home/index',
          })
        }
      })
    } else {
      Message.warning({
        context: this,
        offset: [20, 32],
        duration: 1000,
        content: '请先阅读并勾选用户协议和隐私协议',
      });
    }
  },
  selectConent(event) {
    this.setData({
      isCanFast: event.detail.isConsent
    })
  },
  goPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset['index'] + `?backPageUrl=${this.data.backUrl}`,
    })
  },
  // consentOpen() {
  //   // this.selectComponent('#consent').handlePopup()
  // }
  onLoad(options) {
    let backPaageUrl = options.backPageUrl === 'undefined' ? '' : options.backPageUrl
    this.setData({
      backUrl: backPaageUrl
    })
  }
})